<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="../js/layui/css/layui.css">
<script type="text/javascript" src="../js/layui/layui.js"></script>
<link rel="stylesheet" href="../js/layui_ext/dtree/dtree.css">
<link rel="stylesheet" href="../js/layui_ext/dtree/font/dtreefont.css">
 <script type="text/javascript">
	    layui.extend({
	        dtree: '{/}../js/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
	      }).use(['dtree','layer','jquery'], function(){
	        var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
	        
	        // 初始化树
	        var DemoTree = dtree.render({
	          elem: "#demoTree",//树的id
	          //data: demoTree, // 使用data加载
	          url: "getTreeModules", // 使用url加载（可与data加载同时存在）
	          dataStyle: "layuiStyle",  //使用layui风格的数据格式
        	  dataFormat: "list",  //配置data的风格为list
        	  response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
        	  toolbar:true,//开启工具栏
        	  toolbarScroll:"#toolbarDiv",//工具栏滚动
        	  checkbar:true, //开启复选框
        	  record: true, // 开启数据记录模式
        	  accordion: true, // 开启手风琴
        	  initLevel: 1,
        	  nodeIconArray:{"1":{"open":"dtree-icon-pulldown","close":"dtree-icon-pullup"}},  // 自定扩展的二级非最后一级图标，从1开始
        	  leafIconArray:{"8":"dtree-icon-star"},  // 自定义扩展的二级最后一级图标，从8开始
        	  icon: ["1","8"], // 使用
        	  toolbarStyle: {//修改节点名
         		    title: "模块",
         		    area: ["50%", "400px"]
         		  },
         	   toolbarBtn:[
         		    [{"label":"路径","name":"url","type":"text", "verify": "required"},
         		     {"label":"权重","name":"weight","type":"text", "verify": "required"}/* ,
         		    {"label":"介紹","name":"details","type":"text"} */
         		    ], // 自定义新增页的内容
         		 	[{"label":"路径","name":"url","type":"text","verify": "required"},
         		     {"label":"权重","name":"weight","type":"text","verify": "required"}
         		    ] // 自定义编辑页的内容
         		  ],
          	  toolbarFun: {//工具栏回调函数
          		  	editTreeLoad: function(treeNode){//编辑节点时数据回显
  	        		  	var  param = JSON.parse(treeNode.recordData);
  	        		  	DemoTree.changeTreeNodeDone(param);
          		    },
          		  nowChange : function (treeNode){
          			alert(1);  
          		  },
          		    addTreeNode: function(treeNode, $div){//添加按钮
          		    	treeNode.nodeId = null;//id置空
 						var reg = /^[0-9]*$/;
          		    	//alert(reg.test(treeNode.weight));
          		    	if(!reg.test(treeNode.weight)){
					        	layer.msg("权重不能为空，且只能是数字");
					    		return;
          		    	}else{
          		    	/* alert(treeNode.context)
          		    	alert(treeNode.weight) */
          		      	$.ajax({
          		        type: "post",
          		        data: {
          		        	//id:treeNode.nodeId,
          		        	name:treeNode.context,
          		        	//ops:treeNode.details,
          		        	weight:treeNode.weight,
          		        	path:treeNode.url,
          		        	parentId:treeNode.parentId
          		        },
          		        url: "insertModule",
          		        success: function(res){
          		          if(res.success){
          		        	layer.msg(res.message);
          		          	DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
          		          }else{
          		        	layer.msg(res.message);
          		          	DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
          		          }
          		          
          		        }
          		      }); 
          		     }
          		    },
          		    editTreeNode: function(treeNode, $div){//编辑按钮
          		 
          		    	var reg = /^[0-9]*$/;
          		    	//alert(reg.test(treeNode.weight));
          		    	if(!reg.test(treeNode.weight)){
					        	layer.msg("权重不能为空，且只能是数字");
					    		return;
          		    	}else{
          		    		
          		      $.ajax({
          		        type: "post",
          		        data: {
          		        	id:treeNode.nodeId,
          		        	name:treeNode.context,
          		        	weight:treeNode.weight,
          		        	path:treeNode.url,
          		        	parentId:treeNode.parentId
          		        },
          		        url: "updatetModule",
          		        success: function(res){
          		        	if(res.success){
          		        		layer.msg(res.message);
          		        		DemoTree.changeTreeNodeEdit(true);// 修改成功
              		        	dtree.render({elem: "#demoTree"});//重新渲染树
          		        	}else{
          		        		layer.msg(res.message);
          		        		DemoTree.changeTreeNodeEdit(false);//修改失败
          		        	}
          		        	
          		        
          		        }
          		      });

          		    	}
          		    },
          		    delTreeNode: function(treeNode, $div){//删除按钮
          		      $.ajax({
          		        type: "post",
          		        data: {
          		        	moduleId:treeNode.nodeId
          		        },
          		        url: "delModule",
          		        success: function(res){
          		        	 if(res.success){
		          		        	layer.msg(res.message);
		          		        	//DemoTree.changeTreeNodeAdd("refresh");
		          		        	DemoTree.changeTreeNodeDel(true); // 删除成功
          		        	}else{
          		        		layer.msg(res.message);
          		        		//DemoTree.changeTreeNodeAdd("refresh");
	          		        	DemoTree.changeTreeNodeDel(false); // 删除成功
          		        	}
          		        }
          		      });
          		    }
          		  }
	        });
	        
	        // 绑定节点点击
	        dtree.on("node('demoTree')" ,function(obj){
	          layer.msg(JSON.stringify(obj.param));
	        });
	      });
/* 	    function nowChange(){
	    	alert(12)
	    } */
    </script>
</head>
<body>
<div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                       <!--  <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="uname"  placeholder="请输入模块名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div> -->
                       <!--  <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加模块','./module-add.html',600,400)"><i class="layui-icon"></i>添加</button>
                        </div> -->
                        <div class="layui-card-body layui-table-body layui-table-main">
                        	<div style="height: 450px;overflow: auto;" id="toolbarDiv">
							  <ul id="demoTree" class="dtree" data-id="0"></ul>
							</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</body>
</html>